跳至主要内容

【教程】基於 Ansible 在 Ubuntu 部署控制面 LB

這是一篇開發文件, 面向開發人員以及 AI。

本文以 Ubuntu 為例,使用 Ansible 部署單節點 HAProxy,僅承擔 RKE2/Kubernetes 控制面入口的 L4 TCP 負載。

適用範圍

  • 只承載控制面入口(6443/9345),不負責業務流量或 Ingress。
  • 單節點 LB,不具備高可用。
  • 公有雲無託管 L4 LB,或成本限制時可用。

前置條件

  • 1 臺 Ubuntu 伺服器作為 LB 節點。
  • LB 與控制面節點同地域/同內網,延遲穩定優先。
  • 開放 6443/TCP9345/TCP 到控制面節點。

初始化 Ansible 專案

初始化倉庫

首先建立資料夾,假設專案名稱為 lb-ansible

yun@yun ~/V/a/y/p/ansible (main)> mkdir lb-ansible
yun@yun ~/V/a/y/p/ansible (main)> ls
lb-ansible/

進入專案倉庫,初始化 git, 建立 github 倉庫:

cd lb-ansible
git init
echo "# lb-ansible" > README.md
git add .
git commit -m "chore: initial commit"
gh repo create lb-ansible --private --source=. --remote=origin --push

下面這段程式碼是可選的,用於將新建的程式碼倉庫聲明為子倉庫:

cd ..
rm -rf lb-ansible/

git submodule add https://github.com/<用戶名或組織>/lb-ansible.git ./lb-ansible

規劃目錄結構

接下來劃分專案結構:

mkdir -p inventories/prod \
group_vars \
playbooks \
templates

使用 micro 建立並編輯檔案:

micro ansible.cfg \
inventories/prod/hosts.yml \
group_vars/lb.yml \
playbooks/lb.yml \
templates/haproxy.cfg.j2

目錄結構如下:

lb-ansible/
├── ansible.cfg
├── inventories/
│ └── prod/
│ └── hosts.yml
├── group_vars/
│ └── lb.yml
├── playbooks/
│ └── lb.yml
└── templates/
└── haproxy.cfg.j2

配置 Ansible

micro ansible.cfg :

[defaults]
inventory = inventories/prod/hosts.yml
remote_user = root
host_key_checking = False
timeout = 30

編寫 inventory

micro inventories/prod/hosts.yml :

all:
children:
lb:
hosts:
lb-1:

可直接使用 SSH Config 中的別名,無需再寫 ansible_host

編寫變數

micro group_vars/lb.yml :

haproxy_bind_ip: "0.0.0.0"
haproxy_api_port: 6443
haproxy_reg_port: 9345

rke2_api_backends:
- name: rke2-server1
host: 10.0.0.11
- name: rke2-server2
host: 10.0.0.12
- name: rke2-server3
host: 10.0.0.13

編寫模板

micro templates/haproxy.cfg.j2 :

global
log /dev/log local0
maxconn 4096

defaults
mode tcp
timeout connect 5s
timeout client 30s
timeout server 30s

frontend rke2_api
bind {{ haproxy_bind_ip }}:{{ haproxy_api_port }}
default_backend rke2_api

backend rke2_api
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_api_port }} check
{% endfor %}

frontend rke2_reg
bind {{ haproxy_bind_ip }}:{{ haproxy_reg_port }}
default_backend rke2_reg

backend rke2_reg
option tcp-check
default-server inter 2s fall 3 rise 2
{% for backend in rke2_api_backends %}
server {{ backend.name }} {{ backend.host }}:{{ haproxy_reg_port }} check
{% endfor %}

編寫 Playbook

micro playbooks/lb.yml :

- name: Deploy control plane LB
hosts: lb
become: true
tasks:
- name: Install haproxy
ansible.builtin.apt:
name: haproxy
state: present
update_cache: true

- name: Deploy haproxy config
ansible.builtin.template:
src: templates/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
owner: root
group: root
mode: "0644"
notify: Restart haproxy

- name: Enable haproxy
ansible.builtin.service:
name: haproxy
enabled: true
state: started

handlers:
- name: Restart haproxy
ansible.builtin.service:
name: haproxy
state: restarted

執行與驗證

執行部署:

ansible-playbook playbooks/lb.yml

檢查連接埠:

ss -lntp | grep -E ":6443|:9345"

控制面入口使用 LB 節點 IP 或網域名稱,在 RKE2 變數中將 rke2_api_ip 設為該位址。